/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.persistence.dao;

import edu.hpvas.persistence.entity.Appointment;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import edu.hpvas.persistence.entity.MedicalRecord;
import edu.hpvas.persistence.entity.Veterinarian;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author raider
 */
public class AppointmentDAO implements Serializable {

    public AppointmentDAO() {
        this.emf = Persistence.createEntityManagerFactory("HPVAS");
    }
    private EntityManagerFactory emf;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Appointment appointment) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MedicalRecord idMedicalRecord = appointment.getIdMedicalRecord();
            if (idMedicalRecord != null) {
                idMedicalRecord = em.getReference(idMedicalRecord.getClass(), idMedicalRecord.getId());
                appointment.setIdMedicalRecord(idMedicalRecord);
            }
            Veterinarian idVeterinarian = appointment.getIdVeterinarian();
            if (idVeterinarian != null) {
                idVeterinarian = em.getReference(idVeterinarian.getClass(), idVeterinarian.getId());
                appointment.setIdVeterinarian(idVeterinarian);
            }
            em.persist(appointment);
            if (idMedicalRecord != null) {
                idMedicalRecord.getAppointmentCollection().add(appointment);
                idMedicalRecord = em.merge(idMedicalRecord);
            }
            if (idVeterinarian != null) {
                idVeterinarian.getAppointmentCollection().add(appointment);
                idVeterinarian = em.merge(idVeterinarian);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAppointment(appointment.getId()) != null) {
                System.out.println("Appointment " + appointment + " already exists.");
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

     public List<Appointment> findAppointmentByVet(Veterinarian vet){
        EntityManager em=getEntityManager();
        Query query= em.createQuery("select a from Appointment a where a.idVeterinarian=:vet").setParameter("vet", vet);
        if(!query.getResultList().isEmpty()) {
            return query.getResultList();
        }
        else {
            return null;
        }
    }
    public void edit(Appointment appointment) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Appointment persistentAppointment = em.find(Appointment.class, appointment.getId());
            MedicalRecord idMedicalRecordOld = persistentAppointment.getIdMedicalRecord();
            MedicalRecord idMedicalRecordNew = appointment.getIdMedicalRecord();
            Veterinarian idVeterinarianOld = persistentAppointment.getIdVeterinarian();
            Veterinarian idVeterinarianNew = appointment.getIdVeterinarian();
            if (idMedicalRecordNew != null) {
                idMedicalRecordNew = em.getReference(idMedicalRecordNew.getClass(), idMedicalRecordNew.getId());
                appointment.setIdMedicalRecord(idMedicalRecordNew);
            }
            if (idVeterinarianNew != null) {
                idVeterinarianNew = em.getReference(idVeterinarianNew.getClass(), idVeterinarianNew.getId());
                appointment.setIdVeterinarian(idVeterinarianNew);
            }
            appointment = em.merge(appointment);
            if (idMedicalRecordOld != null && !idMedicalRecordOld.equals(idMedicalRecordNew)) {
                idMedicalRecordOld.getAppointmentCollection().remove(appointment);
                idMedicalRecordOld = em.merge(idMedicalRecordOld);
            }
            if (idMedicalRecordNew != null && !idMedicalRecordNew.equals(idMedicalRecordOld)) {
                idMedicalRecordNew.getAppointmentCollection().add(appointment);
                idMedicalRecordNew = em.merge(idMedicalRecordNew);
            }
            if (idVeterinarianOld != null && !idVeterinarianOld.equals(idVeterinarianNew)) {
                idVeterinarianOld.getAppointmentCollection().remove(appointment);
                idVeterinarianOld = em.merge(idVeterinarianOld);
            }
            if (idVeterinarianNew != null && !idVeterinarianNew.equals(idVeterinarianOld)) {
                idVeterinarianNew.getAppointmentCollection().add(appointment);
                idVeterinarianNew = em.merge(idVeterinarianNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = appointment.getId();
                if (findAppointment(id) == null) {
                    System.out.println("The appointment with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id){
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Appointment appointment = null;
            try {
                appointment = em.getReference(Appointment.class, id);
                appointment.getId();
            } catch (EntityNotFoundException enfe) {
                System.out.println("The appointment with id " + id + " no longer exists.");
            }
            MedicalRecord idMedicalRecord = appointment.getIdMedicalRecord();
            if (idMedicalRecord != null) {
                idMedicalRecord.getAppointmentCollection().remove(appointment);
                idMedicalRecord = em.merge(idMedicalRecord);
            }
            Veterinarian idVeterinarian = appointment.getIdVeterinarian();
            if (idVeterinarian != null) {
                idVeterinarian.getAppointmentCollection().remove(appointment);
                idVeterinarian = em.merge(idVeterinarian);
            }
            em.remove(appointment);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Appointment> findAppointmentEntities() {
        return findAppointmentEntities(true, -1, -1);
    }

    public List<Appointment> findAppointmentEntities(int maxResults, int firstResult) {
        return findAppointmentEntities(false, maxResults, firstResult);
    }

    private List<Appointment> findAppointmentEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Appointment.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Appointment findAppointment(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Appointment.class, id);
        } finally {
            em.close();
        }
    }

    public int getAppointmentCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Appointment> rt = cq.from(Appointment.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
